package com.shse.sale.web.controller;

import com.shse.sale.meta.Content;
import com.shse.sale.meta.User;
import com.shse.sale.service.ContentService;
import com.shse.sale.service.TrxService;
import com.shse.sale.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by shse on 3/18/2016.
 * 综合controller
 */
@Controller
public class MainController {

    /**
     * service注入
     */
    @Autowired
    private UserService userService;
    @Autowired
    private ContentService contentService;
    @Autowired
    private TrxService trxService;

    /**
     * 提供登陆界面
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    /**
     * 注销
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "login";
    }

    /**
     * 首页，显示商品列表
     * 可通过传入type==1来显示未购买商品
     *
     * @param map
     * @param session
     * @param isSell
     * @return
     */
    @RequestMapping("/")
    public String index(ModelMap map,
                        HttpServletRequest request,
                        HttpSession session,
                        @RequestParam(value = "type", required = false) Integer isSell) {
        User user = (User) session.getAttribute("user");
        List<Content> contentList = null;
        if (user != null && isSell != null && isSell == 1) {
            contentService.getUnBuyContents(request, map, user);
        } else {
            contentService.getContents(request, map, user);
        }
        return "index";
    }

    /**
     * 单个商品页面
     *
     * @param id
     * @param session
     * @param map
     * @return
     */
    @RequestMapping("/show")
    public String show(@RequestParam("id") String id,
                       HttpSession session,
                       ModelMap map) {
        User user = (User) session.getAttribute("user");
        Content content = contentService.getContentById(id);
        map.addAttribute("product", content);
        return "show";
    }

    /**
     * 账户页，显示买家购买商品及金额
     *
     * @param session
     * @param map
     * @return
     */
    @RequestMapping("/account")
    public String account(HttpSession session,
                          ModelMap map) {
        User user = (User) session.getAttribute("user");

        //若未登陆，重定向至登陆界面
        if (user == null || user.getUsertype() != 0) {
            return "redirect:/login";
        }
        List<Content> buyList = contentService.getBuyedContents(user);
        map.addAttribute("buyList", buyList);
        return "account";
    }

    /**
     * 提供发布页面，需要登录
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/public", method = RequestMethod.GET)
    public String publish(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || user.getUsertype() == 0) {
            return "redirect:/login";
        }
        return "public";
    }

    /**
     * 处理发布请求
     *
     * @param map
     * @param session
     * @param title
     * @param detail
     * @param price
     * @param summary
     * @param picPath
     * @return
     */
    @RequestMapping(value = "/publicSubmit", method = RequestMethod.POST)
    public String publish(ModelMap map,
                          HttpSession session,
                          @RequestParam("title") String title,
                          @RequestParam("detail") String detail,
                          @RequestParam("price") String price,
                          @RequestParam("summary") String summary,
                          @RequestParam("picPath") String picPath) {
        User user = (User) session.getAttribute("user");
        if (user != null && user.getUsertype() == 1) {
            Content content = new Content();
            content.setImage(picPath);
            content.setTitle(title);
            content.setDetail(detail);
            content.setPrice(Integer.parseInt(price));
            content.setSummary(summary);

            String message = contentService.addContent(content);
            if(message.equals("正常发布")){
                map.addAttribute("product", content);
            }
        }
        return "publicSubmit";
    }

    /**
     * 提供编辑页面，需要登录
     *
     * @param id
     * @param map
     * @param session
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(@RequestParam("id") String id,
                       ModelMap map,
                       HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user == null || user.getUsertype() == 0) {
            return "redirect:/login";
        }
        Content content = contentService.getContentById(id);
        map.addAttribute("product", content);
        return "edit";
    }

    /**
     * 处理编辑请求
     *
     * @param map
     * @param session
     * @param id
     * @param title
     * @param detail
     * @param price
     * @param summary
     * @param picPath
     * @return
     */
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST)
    public String eidt(ModelMap map,
                       HttpSession session,
                       @RequestParam("id") String id,
                       @RequestParam("title") String title,
                       @RequestParam("detail") String detail,
                       @RequestParam("price") String price,
                       @RequestParam("summary") String summary,
                       @RequestParam(value = "picPath", required = false) String picPath) {
        User user = (User) session.getAttribute("user");
        if (user != null && user.getUsertype() == 1) {
            Content content = contentService.getContentById(id);
            content.setImage(picPath);
            content.setId(Integer.parseInt(id));
            content.setTitle(title);
            content.setDetail(detail);
            content.setPrice(Integer.parseInt(price));
            content.setSummary(summary);

            String message = contentService.updateContent(content);
            if(message.equals("正常更新")){
                map.addAttribute("product", content);
            }
        }
        return "editSubmit";
    }


}
